package Arraylist集合存储学生对象并遍历;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ArraylistDemo {
    public static void main(String[] args) {
        ArrayList<Student>s=new ArrayList<Student>();
        Student s1=new Student("姜恒星",30);
        Student s2=new Student("姜恒",20);
        Student s3=new Student("姜",10);

        //添加到集合
        s.add(s1);
        s.add(s2);
        s.add(s3);

        //通过迭代器遍历
        Iterator<Student> it = s.iterator();
        while(it.hasNext()){
            Student stu = it.next();
            System.out.println(stu.getName()+","+stu.getAge());
        }

        //使用for循环遍历
        for (int i = 0; i <s.size() ; i++) {
            Student stu = s.get(i);
            System.out.println(stu.getName()+","+stu.getAge());
        }

        //使用增强for循环
        for(Student stu:s){
            System.out.println(stu.getName()+","+stu.getAge());
        }
    }
}
